package com.c2c.mapper;

import com.c2c.po.Goods;
import com.c2c.po.GoodsClassify;
import com.c2c.po.User;
import com.c2c.utils.SQLProviderUtil;
import org.apache.ibatis.annotations.*;
import java.util.List;

public interface GoodsMapper {

    /**
     * 查找所有商品
     * @return      所有商品
     */
    @Select("select * from goods ORDER BY goodsCategoryId, goodsId DESC")
    //貌似配置部分也生效........所以这么偷懒了  其实我是配全了的
    @Results( id = "Goods", value = {
            @Result(id = true,column = "goodsId", property = "goodsId"),//nm  不加这一句查到返回的所有goodsId全是0
            @Result(
                    column = "userAccount",//根据哪一个字段查询数据
                    property = "user",//要封装的属性名称
                    javaType = User.class,//要封装的实体类型
                    //select属性  代表查询那个接口的方法获得数据
                    one = @One(select = "com.c2c.mapper.UserMapper.findByUserId")
            ),
            @Result(
                    column = "goodsId",//根据哪一个字段查询数据
                    property = "comment",//要封装的属性名称
                    javaType = List.class,//要封装的实体类型
                    //select属性  代表查询那个接口的方法获得数据
                    many = @Many(select = "com.c2c.mapper.CommentMapper.findByGoodsId")
            ),
            @Result(
                    column = "goodsCategoryId",//根据哪一个字段查询表中数据
                    property = "goodsClassify",//要封装的属性名称
                    javaType = GoodsClassify.class,//要封装的实体类型
                    //select属性  代表查询那个接口的方法获得数据
                    one = @One(select = "com.c2c.mapper.GoodsClassifyMapper.findByClassifyId")
            )
    })
    List<Goods> findAllGoods();

    /**
     * 根据Id查找固定商品
     * @param goodId  商品Id
     * @return        对应商品
     */
    @Select("select * from goods where goodsId = #{goodsId}")
    @ResultMap("Goods")
    Goods findById(int goodId);

    /**
     * 根据用户Id查找其售卖商品
     * @param     userAccount   用户Id
     * @return                  对应商品
     */
    @Select("select * from goods where userAccount = #{userAccount}")
    @ResultMap("Goods")
    List<Goods> findByUserId(int userAccount);


    /**
     * 根据商品类型查找对应商品
     * @param goodsClassifyId    商品类别ID
     * @return                   返回查找到的内容
     */
    @Select("select * from goods where goodsCategoryId = #{goodsClassifyId}")
    @ResultMap("Goods")
    List<Goods> findByClassifyId(int goodsClassifyId);

    /**
     * 拼装语句
     * 模糊查询 加 多条件查询
     * @param string   搜索内容
     * @param goodsClassifyId    商品类别
     * @return         获取结果
     */
    @SelectProvider(type = SQLProviderUtil.class, method = "findBySomething")
    @ResultMap("Goods")
    List<Goods> findBySomething(@Param("string") String string,@Param("goodsClassifyId") int goodsClassifyId);

    /**
     * 商品发布者修改商品  但是修改后会更改状态为未审核
     * @param goods  封装内容
     * */
    @Update("update goods set " +
            "goodsName = #{goodsName} , " +
            "goodsPrice = #{goodsPrice} , " +
            "goodsIntroduce = #{goodsIntroduce} , " +
            "goodsTrait = #{goodsTrait} , " +
            "goodsImg = #{goodsImg} , " +
            "goodsCondition = #{goodsCondition} , " +
            "goodsCategoryId = #{goodsClassify.goodsCategoryId} , " +
            "goodsState = 1 where goodsId = #{goodsId}")
    void updateAllById(Goods goods);

    /**
     * 更改商品状态
     * @param goods  封装内容
     */
    @Update("update goods set goodsState = #{goodsState} where goodsId = #{goodsId}")
    void updateStateById(Goods goods);

    /**
     * 新增商品
     * @param goods 封装信息*/
    @Insert("insert into goods values (#{goodsId},#{goodsName},#{goodsPrice}," +
            "#{goodsIntroduce},#{goodsTrait},#{goodsImg},#{goodsShelfTime},#{goodsCondition}," +
            "1,#{user.userAccount},#{goodsClassify.goodsCategoryId})")
    @Options(useGeneratedKeys = true, keyProperty = "goodsId", keyColumn = "goodsId")//返回自增主键
    void insertGoods(Goods goods);
}
